package com.innovatio.validator.handlers;

import com.innovatio.validator.annotations.ValidatePattern;
import com.innovatio.validator.ValidationErrors;
import com.innovatio.validator.Validable;
import com.innovatio.validator.exceptions.ValidatorException;

import java.util.regex.Pattern;

/**
 * This code is property of Innovatio Software Solutions, Inc.
 * User: Edxe
 * Date: Jul 7, 2009
 * Time: 9:17:08 PM
 */
public class ValidatePatternHandler implements ValidationHandler<ValidatePattern>{


    @Override
    public ValidationErrors.Error validate(ValidatePattern annotation, Validable container, String fieldName, String value) throws ValidatorException {

        String pattern = annotation.patter();

        if (value == null || !Pattern.matches(pattern, value)){
            return new ValidationErrors.Error(fieldName,annotation.i18nBundleKey());
        }


        return null;
    }

}
